package com.xuanyuan.material

import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.xuanyuan.base.BaseActivity
import com.xuanyuan.material.databinding.ActivityBottomNavigationBinding
import com.xuanyuan.navaction.Fragment2
import com.xuanyuan.navaction.Fragment3
import com.xuanyuan.navaction.FragmentMaterial


class BottomNavigationActivity : BaseActivity<ActivityBottomNavigationBinding>() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun initLayout(): Int {
        return R.layout.activity_bottom_navigation
    }


    fun initView() {

        var fragments: MutableList<Fragment> = ArrayList()
        fragments.add(FragmentMaterial())
        fragments.add(Fragment2())
        fragments.add(Fragment3())
        val adapter = object : FragmentStateAdapter(this) {

            override fun getItemCount(): Int {
                return fragments.size
            }

            override fun createFragment(position: Int): Fragment {
                return Fragment3()
            }
        }

        mBinding.bottomNV.setOnNavigationItemSelectedListener { item ->
            when (item.itemId) {
                R.id.option_normal_1 -> {
                    true
                }
                R.id.option_normal_1 -> {
                    true
                }
                else -> false
            }
        }

        mBinding.viewPage2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels)
                val menuItem = mBinding.bottomNV.menu.getItem(position)
                menuItem.isChecked = true
            }
        })

        //禁止ViewPager滑动
        mBinding.viewPage2.setOnTouchListener(object : View.OnTouchListener {
            override fun onTouch(v: View?, event: MotionEvent?): Boolean {
                return true
            }
        })

    }

    override fun afterInit() {

    }


}

